<?php

//TODO: ensure that you can't invite someone to a project which doesn't
//exist or add someone to a meeting which doesn't exist or add a comment to
//a task which doesn't exist or assign a task to a project which doesn't
//exist or add a member to a project which doesn't exist or add a contact
//to another contact which doesn't exist

/*
Summary

The following functions are contained in this file:

Tasks:
addTask
editTask
setTaskCompleted
setTaskDisabled
setTaskHelpRequired
setTaskPagesRemaining
setTaskProblemsRemaining
setTaskTimeToCompleteInMinutes
deleteTask
deleteAllTasksByUser
deleteAllTasksFromProject
addComment
deleteComment
deleteCommentsByTask
addTaskDependency
deleteTaskDependency
deleteTaskDependenciesByTask
computeTaskDisabledBasedOnDependencies - Call this before displaying tasks

Users:
addUser
editUser
editUserPassword
checkUserPassword
deleteUser
getUsernameFromUserId
getUserIdFromUsername
addContact
deleteContact
deleteAllContactsByUser

Projects:
addProject
editProject
deleteProject
checkIsUserInProject
addProjectInvitation
deleteProjectInvitation - Avoid using this, use accept/reject instead
acceptProjectInvitation
rejectProjectInvitation
deleteAllProjectInvitationsByUser
deleteAllProjectInvitationsFromProject
deleteUserFromProject
deleteUserFromAllProjects
deleteAllUsersFromProject

Meetings:
addMeeting
editMeeting
deleteMeeting
addUserToMeeting
deleteUserFromMeeting
deleteUserFromAllMeetings

Display:
displayTasks
displayTaskComments
displayProjects
displayProjectTasks
displayProjectInvitations
displayProjectMembers
displayMeetings
displayContacts
magicTodo
*/

/*
Inputs:
task_name - The name of this task.
time_created - The time which the task was added to the database.
due_time_from - The beginning of the time range in which this task is due.
due_time_to - The end of the time range in which this task is due.
user_id - The id of the user who created this task.
project_id - The id of the project which this task will be added to.
assigned_to - The id of the user who is required to complete this task.
help_required - This value is set to 0 if help is not required on this task or
	1 if help is required.
completed - This value is 0 if the task is not completed or 1 if it is
	completed.
disabled - This value is 0 if the task is enabled or 1 if it is
	disabled (grayed out).
pages - The number of pages in total which must be read for this task.
problems - The number of problems in total which must be completed for
	this task.
pages_remaining - The number of pages remaining on this task.
problems_remaining - The number of problems remaining on this task.
time_to_complete_in_minutes - The estimated time required to complete this
	task in minutes.
Process:
This function adds a task to the tasks table.
Output:
task_id - The id assigned to the task.
*/
function addTask($task_name,$time_created,$due_time_from,$due_time_to,
$user_id,$project_id=null,$assigned_to=null,$help_required=0,
$completed=0,$disabled=0,$pages=null,$problems=null,$pages_remaining=null,
$problems_remaining=null,$time_to_complete_in_minutes=null) {
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "INSERT INTO tasks(task_id,task_name,time_created,
			due_time_from,due_time_to,user_id,project_id,
			assigned_to,help_required,completed,disabled,pages,
			problems,pages_remaining,problems_remaining,
			time_to_complete_in_minutes,deleted) 
			VALUES(null,'%s','%s','%s','%s',
			%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0)";
		$replaced = sprintf($command,$task_name,$time_created,
			$due_time_from,$due_time_to,$user_id,$project_id,
			$assigned_to,$help_required,$completed,$disabled,$pages,
			$problems,$pages_remaining,$problems_remaining,
			$time_to_complete_in_minutes);
		$db->queryExec($replaced);
		$command = "SELECT MAX(task_id) FROM tasks";
		$result = $db->query($command);
		$cur = $result->current();
		$task_id = $cur['MAX(task_id)'];
		return $task_id;
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task being edited.
task_name - The edited name of the task.
task_name_edit - Set this to 1 to edit the task name, 0 otherwise.
due_time_from - The edited beginning of the due time range.
due_time_from_edit - Set this to 1 to edit the beginning of the due time range,
	0 otherwise.
due_time_to - The edited end of the due time range.
due_time_to_edit - Set this to 1 to edit the end of the due time range,
	0 otherwise.
project_id - The edited project the task belongs to.
project_id_edit - Set this to 1 to edit the project the task belongs to,
	0 otherwise.
assigned_to - The edited user the task is assigned to.
assigned_to_edit - Set this to 1 to edit the user the task is assigned to,
	0 otherwise.
pages - The edited number of the pages to be read for the task
pages_edit - Set this to 1 to edit the number of pages to be read,
	0 otherwise.
problems - The edited number of problems to be completed for the task.
problems_edit - Set this to 1 to edit the number of problems to be completed
	for the task, 0 otherwise.
Process:
This function edits an existing task.
Output:
This function has no output.
*/
function editTask($task_id,$task_name=null,$task_name_edit=null,
$due_time_from=null,$due_time_from_edit=null,$due_time_to=null,
$due_time_to_edit=null,$project_id=null,$project_id_edit=null,
$assigned_to=null,$assigned_to_edit=null,$pages=null,$pages_edit=null,
$problems=null,$problems_edit=null){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT COUNT(*) FROM tasks
			WHERE task_id=%d AND deleted=0";
		$replaced = sprintf($command,$task_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']!=0){
			// This task exists
			$command = "UPDATE tasks SET ";
			$added=false;
			if($task_name_edit!=null){
				$added=true;
				$command = $command."task_name='".$task_name.
					"' ";
			}
			if($due_time_from_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."due_time_from='".
					$due_time_from."' ";
			}
			if($due_time_to_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."due_time_to='".
					$due_time_to."' ";
			}
			if($project_id_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."project_id=".
					$project_id." ";
			}
			if($assigned_to_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."assigned_to=".
					$assigned_to." ";
			}
			if($pages_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."pages=".$pages." ";
			}
			if($problems_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."problems=".$problems." ";
			}
			$command = $command."WHERE task_id=".$task_id.
				" AND deleted=0";
			$db->queryExec($command);
		} else {
			// This task does not exist,
			// don't do anything
		}
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task which has been completed.
completed - Set this to 1 if the task has been completed, 0 otherwise.
Process:
Sets a task to completed.
Output:
This function has no output.
*/
function setTaskCompleted($task_id,$completed=1){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE tasks SET completed=%d 
			WHERE task_id=%d AND deleted=0";
		$replaced = sprintf($command,$completed,$task_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task to be disabled.
disabled - Set this to 1 if the task is to be disabled, 0 otherwise.
Process:
Sets whether the task is disabled.
Output:
This function has no output.
*/
function setTaskDisabled($task_id,$disabled=1){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE tasks SET disabled=%d 
			WHERE task_id=%d AND deleted=0";
		$replaced = sprintf($command,$disabled,$task_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task for which help is required.
help_required - Set this to 1 if help is required, 0 otherwise.
Process:
Sets whether help is required on a task or not.
Output:
This function has no output.
*/
function setTaskHelpRequired($task_id,$help_required=1){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE tasks SET help_required=%d 
			WHERE task_id=%d AND deleted=0";
		$replaced = sprintf($command,$help_required,$task_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task to be edited.
pages_remaining - The edited number of pages remaining in the task.
Process:
This function sets the number of pages remaining for a task.
Output:
This function has no output.
*/
function setTaskPagesRemaining($task_id,$pages_remaining){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE tasks SET pages_remaining=%d 
			WHERE task_id=%d AND deleted=0";
		$replaced = sprintf($command,$pages_remaining,$task_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task to be edited.
problems_remaining - The edited number of problems remaining in the task.
Process:
This function sets the number of problems remaining in the task.
Output:
This function has no output.
*/
function setTaskProblemsRemaining($task_id,$problems_remaining){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE tasks SET problems_remaining=%d 
			WHERE task_id=%d AND deleted=0";
		$replaced = sprintf($command,$problems_remaining,$task_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task to be edited.
time_to_complete_in_minutes - The edited time to complete the task in minutes.
Process:
This function sets the time remaining to complete the task in minutes.
Output:
This function has no output.
*/
function setTaskTimeToCompleteInMinutes($task_id,$time_to_complete_in_minutes){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE tasks SET time_to_complete_in_minutes=%d 
			WHERE task_id=%d AND deleted=0";
		$replaced = sprintf($command,$time_to_complete_in_minutes,
			$task_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task to be deleted.
Process:
This function deletes a task from the tasks table and deletes all of
its comments from the task_comments table.
This function also deletes all of the dependencies related to the task.
Output:
This function has no output.
*/
function deleteTask($task_id){
	// Delete all of the task's comments
	deleteCommentsByTask($task_id);
	// Delete all of the task's dependencies
	deleteTaskDependenciesByTask($task_id);
	// Delete the task
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE tasks SET deleted=1 WHERE task_id=%d";
		$replaced = sprintf($command,$task_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The user whose tasks are being deleted.
Process:
This function deletes all of a user's tasks which are not part of a project.
Output:
This function has no output.
*/
function deleteAllTasksByUser($user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM tasks WHERE user_id=%d 
			AND project_id=0 AND deleted=0";
		$replaced = sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteTask($cur['task_id']);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Input:
project_id - The id of the project from which all tasks are being deleted.
Process:
This function deletes all of the tasks contained within a specific project.
Output:
This function has no output.
*/
function deleteAllTasksFromProject($project_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM tasks WHERE project_id=%d
		AND deleted=0";
		$replaced = sprintf($command,$project_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteTask($cur['task_id']);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task to which the comment is being added.
user_id - The id of the user who added the comment.
comment - The text of the comment.
time - The time when the comment was added to the database.
Process:
This function adds a comment to the task_comments table.
Output:
comment_id - The id of the comment.
*/
function addComment($task_id,$user_id,$comment,$time){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "INSERT INTO task_comments(comment_id,task_id,
			user_id,comment,time,deleted)
			VALUES(null,%d,%d,'%s','%s',0)";
		$replaced = sprintf($command,$task_id,$user_id,$comment,$time);
		$db->queryExec($replaced);
		$command = "SELECT MAX(comment_id) FROM task_comments";
		$result = $db->query($command);
		$cur = $result->current();
		$comment_id = $cur['MAX(comment_id)'];
		return $comment_id;
	} else {
		die($err);
	}
}

/*
Inputs:
comment_id - The id of the comment to be deleted.
Process:
This function deletes a comment on a task.
Output:
This function has no output.
*/
function deleteComment($comment_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE task_comments SET deleted=1 
			WHERE comment_id=%d";
		$replaced = sprintf($command,$comment_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task which will have all of its comments deleted.
Process:
This function deletes all of a task's comments.
Output:
This function has no output.
*/
function deleteCommentsByTask($task_id){
if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE task_comments SET deleted=1 
			WHERE task_id=%d";
		$replaced = sprintf($command,$task_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The id of the task which will be dependent on another task.
dependency_id - The id of the task which will be depended upon.
Process:
This function adds a dependency of one task(task_id) on another(dependency_id).
Output:
This function has no output.
*/
function addTaskDependency($task_id,$dependency_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Check if the dependency already exists
		$command = "SELECT COUNT(*) FROM task_dependencies
			WHERE task_id=%d AND dependency_id=%d";
		$replaced = sprintf($command,$task_id,$dependency_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']==0){
			// The dependency does not exist
			$command = "INSERT INTO task_dependencies(task_id,
				dependency_id,deleted) 
				VALUES(%d,%d,'%s',0)";
			$replaced = sprintf($command,$task_id,$dependency_id);
			$db->queryExec($replaced);
		} else {
			// The dependency already exists, check if it is deleted
			$command="SELECT * FROM task_dependencies
				WHERE task_id=%d AND dependency_id=%d";
			$replaced = sprintf($command,$task_id,$dependency_id);
			$result = $db->query($replaced);
			$cur = $result->current();
			if($cur['deleted']==1){
				// The dependency is deleted, update it
				$command = "UPDATE task_dependencies 
					SET deleted=0
					WHERE task_id=%d AND 
					dependency_id=%d";
				$replaced = sprintf($command,$task_id,
					$dependency_id);
				$db->queryExec($replaced);
			} else {
				// The dependency is not deleted,
				// don't do anything
			}
		}
	} else {
		die($err);
	}
}

/*
Input:
task_id - The id of the task which is dependent.
dependency_id - The id of the task which is being depended upon.
Process:
This function deletes a task dependency.
Output:
This function has no output.
*/
function deleteTaskDependency($task_id,$dependency_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE task_dependencies SET deleted=1 
			WHERE task_id=%d AND dependency_id=%d";
		$replaced = sprintf($command,$task_id,$dependency_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Input:
task_id - The id of the task which will have all of its dependencies deleted.
Process:
This function deletes all of the dependencies on this task and all of the
dependencies that this task has on other tasks.
Output:
This function has no output.
*/
function deleteTaskDependenciesByTask($task_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM task_dependencies WHERE
			(task_id=%d OR dependency_id=%d) AND deleted=0";
		$replaced = sprintf($command,$task_id,$task_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteTaskDependency($cur['task_id'],
				$cur['dependency_id']);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Inputs:
This function has no inputs.
Process:
This function computes the disabled status of all tasks based on whether
their dependencies are completed.
Output:
This function has no output.
*/
function computeTaskDisabledBasedOnDependencies(){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Make every task enabled
		$command = "UPDATE tasks SET disabled=0 WHERE deleted=0";
		$db->queryExec($command);
		// Join tasks and task dependencies
		$command = "SELECT * FROM tasks, task_dependencies
			WHERE tasks.task_id=task_dependencies.task_id AND 
			tasks.deleted=0 AND task_dependencies.deleted=0";
		$result = $db->query($command);
		while($result->valid()){
			// If the task dependency is not completed,
			// set the task to disabled=1
			$cur = $result->current();
			$command = "SELECT * from tasks WHERE task_id=%d AND
				deleted=0";
			$replaced = sprintf($command,
				$cur['task_dependencies.dependency_id']);
			$result = $db->query($replaced);
			$dependencyReturn = $result->current();
			if($dependencyReturn['completed']!=1){
				// Dependency is not completed,
				// set task to disabled
				$command = "UPDATE tasks SET disabled=1
					WHERE task_id=%d";
				$replaced = sprintf($command,
					$cur['tasks.task_id']);
				$db->queryExec($replaced);
			}
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Inputs:
username - The name of this user.
time_created - The time this user was added to the database.
password - The user's password.
email - The user's email address.
tel - The user's landline telephone number.
mobile - The user's mobile telephone number.
avatar - A link to the user's avatar image.
color - The color assigned to the user.
average_pages_per_minute - The average reading speed of the user in pages
	per minute.
average_problems_per_minute - The average rate at which the user can
	complete problems measured per minute.
Process:
This function adds a user to the users table and adds their password
to the passwords table.
Output:
user_id - The id assigned to the user this will be null if the username
	already exists.
*/
function addUser($username,$time_created,$password,$email=null,$tel=null,
$mobile=null,$avatar=null,$color=null,$average_pages_per_minute=null,
$average_problems_per_minute=null){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Check if username already exists in a non-deleted user
		$command = "SELECT COUNT(*) FROM users WHERE username='%s' 
			AND deleted=0";
		$replaced = sprintf($command,$username);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']==0){
			// No such username exists, insert new user
			$command = "INSERT INTO users(user_id,username,
				time_created,email,tel,mobile,avatar,color,
				average_pages_per_minute,
				average_problems_per_minute,
				deleted) VALUES(null,'%s','%s','%s','%s','%s',
				'%s','%s',%f,%f,0)";
			$replaced = sprintf($command,$username,$time_created,
			$email,$tel,$mobile,$avatar,$color,
			$average_pages_per_minute,$average_problems_per_minute);
			$db->queryExec($replaced);
			$command = "SELECT MAX(user_id) FROM users";
			$result = $db->query($command);
			$cur = $result->current();
			$user_id = $cur['MAX(user_id)'];
			$command = "INSERT INTO passwords(user_id,password,
				deleted) VALUES(%d,'%s',0)";
			$replaced = sprintf($command,$user_id,$password);
			$db->queryExec($replaced);
		} else {
			// Username exists, check whether user is deleted
			$command = "SELECT * FROM users 
				WHERE username='%s'";
			$replaced = sprintf($command,$username);
			$result = $db->query($replaced);
			$cur = $result->current();
			if($cur['deleted']==1){
				// The user has been deleted	
				$command = "UPDATE users SET time_created='%s',
					email='%s',tel='%s',mobile='%s',
					avatar='%s',color='%s',
					average_pages_per_minute=%f,
					average_problems_per_minute=%f,
					deleted=0 WHERE username='%s'";
				$replaced = sprintf($command,$time_created,
					$email,$tel,$mobile,$avatar,$color,
					$average_pages_per_minute,
					$average_problems_per_minute,
					$username);
				$db->queryExec($replaced);
				$user_id = $cur['user_id'];
				$command = "UPDATE passwords SET password='%s'
					WHERE user_id=%d";
				$replaced = sprintf($command,$password,
					$user_id);
				$db->queryExec($replaced);
			} else {
				// The user has not been deleted,
				// don't do anything.
				$user_id=null;
			}
		}
		return $user_id;
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The id of the user whose details are being edited.
email - The edited email address of the user.
email_edit - Set this to 1 to edit the email address of the user, 0 otherwise.
tel - The edited telephone number of the user.
tel_edit - Set this to 1 to edit the telephone number of the user,
	0 otherwise.
mobile - The edited mobile phone number of the user.
mobile_edit - Set this to 1 to edit the mobile phone number of the user,
	0 otherwise.
avatar - The edited address of the avatar of the user.
avatar_edit - Set this to 1 to edit the avatar of the user,
	0 otherwise.
color - The edited color assigned to the user.
color_edit - Set this to 1 to edit the color assigned to the user,
	0 otherwise.
average_pages_per_minute - The edited average pages per minute of the user.
$average_pages_per_minute_edit - Set this to 1 to edit the average pages per
	minute of the user, 0 otherwise.
average_problems_per_minute - The edited average problems per minute of the 
	user.
average_problems_per_minute_edit - Set this to 1 to edit the average
	problems per minute of the user, 0 otherwise.
Process:
This function edits a specific user's information.
Output:
This function has no output.
*/
function editUser($user_id,$email=null,$email_edit=null,$tel=null,
$tel_edit=null,$mobile=null,$mobile_edit=null,$avatar=null,$avatar_edit=null,
$color=null,$color_edit=null,$average_pages_per_minute=null,
$average_pages_per_minute_edit=null,$average_problems_per_minute=null,
$average_problems_per_minute_edit=null){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT COUNT(*) FROM users
			WHERE user_id=%d AND deleted=0";
		$replaced = sprintf($command,$user_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']!=0){
			// This user exists
			$command = "UPDATE users SET ";
			$added=false;
			if($email_edit!=null){
				$added=true;
				$command = $command."email='".$email.
					"' ";
			}
			if($tel_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."tel='".$tel.
					"' ";
			}
			if($mobile_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."mobile='".$mobile.
					"' ";
			}
			if($avatar_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."avatar='".$avatar.
					"' ";
			}
			if($color_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."color='".$color.
					"' ";
			}
			if($average_pages_per_minute_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."average_page_per_minute=".
					$average_pages_per_minute." ";
			}
			if($average_problems_per_minute_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command.
					"average_problems_per_minute=".
					$average_pages_per_minute." ";
			}
			$command = $command."WHERE user_id=".$user_id.
				" AND deleted=0";
			$db->queryExec($command);
		} else {
			// This user does not exist,
			// don't do anything
		}
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The id of the user whose password is being edited.
password - The edited password of the user.
Process:
This function edits the user's password.
Output:
This function has no output.
*/
function editUserPassword($user_id,$password){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT COUNT(*) FROM passwords
			WHERE user_id=%d AND deleted=0";
		$replaced = sprintf($command,$user_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']!=0){
			// This user exists
			$command = "UPDATE passowords SET ";
			$command = $command."password='".$password."' ";
			$command = $command."WHERE user_id=".$user_id.
				" AND deleted=0";
			$db->queryExec($command);
		} else {
			// This user does not exist,
			// don't do anything
		}
	} else {
		die($err);
	}
}

/*
Inputs:
username - The name of the user.
password - The password which is being checked.
Process:
This function checks a user's password and returns the user's id if their 
password is correct.
Output:
user_id - This function returns the user's id or 0 if there is no user or
their password is incorrect.
*/
function checkUserPassword($username,$password){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM users, passwords
			WHERE users.user_id=passwords.user_id
			AND users.username='%s' AND passwords.password='%s'
			AND users.deleted=0 AND passwords.deleted=0";
		$replaced = sprintf($command,$username,$password);
		$result = $db->query($replaced);
		$cur = $result->current();
		return $cur['users.user_id'];
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The id of the user to be deleted.
Process:
This function deletes a user from the users table, deletes all of the tasks
they created which aren't assigned to a project, deletes the user from 
the password table, removes them from all of the projects they are
a member of, removes them from all contact lists, deletes any meetings they
have created and removes them from any meetings they are invited to and
deletes any project invitations they have outstanding.
Also, if the user was the only member in any meeting or project, this
function also deletes those projects or meetings.
Output:
This function has no output.
*/
function deleteUser($user_id){
	// Delete the tasks assigned to the user which are not part of
	// a project
	deleteAllTasksByUser($user_id);
	// Delete the user from all of the projects they are a member of
	deleteUserFromAllProjects($user_id);
	// Delete the user from all contact lists
	deleteAllContactsByUser($user_id);
	// Delete the user from all meetings they are a member of
	deleteUserFromAllMeetings($user_id);
	// Delete all project invitations outstanding for the user
	deleteAllProjectInvitationsByUser($user_id);
	// Delete the user
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Delete the user from the users table
		$command = "UPDATE users SET deleted=1 WHERE user_id=%d";
		$replaced = sprintf($command,$user_id);
		$db->queryExec($replaced);
		// Delete the user from the passwords table
		$command = "UPDATE passwords SET deleted=1 WHERE user_id=%d";
		$replaced = sprintf($command,$user_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The id of the user.
Process:
This function converts a user id into a user name.
Output:
username - The name of the user.
*/
function getUsernameFromUserId($user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT username FROM users WHERE
			user_id=%d AND deleted=0";
		$replaced = sprintf($command,$user_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		return $cur['username'];
	} else {
		die($err);
	}
}

/*
Inputs:
username - The name of the user.
Process:
This function converts a user name into a user id.
Output:
user_id - The id of the user.
*/
function getUserIdFromUsername($username){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT user_id FROM users WHERE
			username=%s AND deleted=0";
		$replaced = sprintf($command,$username);
		$result = $db->query($replaced);
		$cur = $result->current();
		return $cur['user_id'];
	} else {
		die($err);
	}
}


/*
Inputs:
user_id - The id of the user who is adding the contact.
contact_user_id - The id of the user who is being added to the contact
	list.
time_connected - The time the contacts were added to the database.
Process:
This function adds two contacts to the contact_list database, in both
directions.
Output:
This function has no output.
*/
function addContact($user_id,$contact_user_id,$time_connected){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Check if the contact already exists
		$command = "SELECT COUNT(*) FROM contact_list
			WHERE user_id=%d AND contact_user_id=%d";
		$replaced = sprintf($command,$user_id,$contact_user_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']==0){
			// The contact does not exist
			$command = "INSERT INTO contact_list(user_id,
				contact_user_id,time_connected,deleted) 
				VALUES(%d,%d,'%s',0)";
			$replaced = sprintf($command,$user_id,$contact_user_id,
			$time_connected);
			$db->queryExec($replaced);
			$command = "INSERT INTO contact_list(user_id,
				contact_user_id,time_connected,deleted)
				VALUES(%d,%d,'%s',0)";
			$replaced = sprintf($command,$contact_user_id,$user_id,
			$time_connected);
			$db->queryExec($replaced);
		} else {
			// The contact already exists, check if it is deleted
			$command="SELECT * FROM contact_list
				WHERE user_id=%d AND contact_user_id=%d";
			$replaced = sprintf($command,$user_id,$contact_user_id);
			$result = $db->query($replaced);
			$cur = $result->current();
			if($cur['deleted']==1){
				// The contact is deleted, update it
				$command = "UPDATE contact_list 
					SET time_connected='%s',deleted=0
					WHERE user_id=%d AND 
					contact_user_id=%d";
				$replaced = sprintf($command,$time_connected,
					$user_id,$contact_user_id);
				$db->queryExec($replaced);
				$command = "UPDATE contact_list 
					SET time_connected='%s',deleted=0
					WHERE user_id=%d AND 
					contact_user_id=%d";
				$replaced = sprintf($command,$time_connected,
					$contact_user_id,$user_id);
				$db->queryExec($replaced);
			} else {
				// The contact is not deleted,
				// don't do anything
			}
		}
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The id of the user who is deleting the contact.
contact_user_id - The id of the user whose contact is being deleted.
Process:
This function deletes two contacts from the contact_list database,
in both directions.
Output:
This function has no output.
*/
function deleteContact($user_id,$contact_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE contact_list SET deleted=1 WHERE user_id=%d
		AND contact_user_id=%d";
		$replaced = sprintf($command,$user_id,$contact_user_id);
		$db->queryExec($replaced);
		$command = "UPDATE contact_list SET deleted=1 WHERE user_id=%d
		AND contact_user_id=%d";
		$replaced = sprintf($command,$contact_user_id,$user_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The id of the user whose contacts are to be deleted.
Process:
This function deletes all of the contacts which contain the user's id.
Output:
This function has no output.
*/
function deleteAllContactsByUser($user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM contact_list WHERE
			user_id=%d AND deleted=0";
		$replaced = sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteContact($user_id,$cur['contact_user_id']);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Inputs:
project_name - The name of this project.
time_created - The time the project was added to the database.
user_id - The id of the user who created this project.
description - A text description of the project.
Process:
This function adds a project to the projects table. It also adds the user
who created the project as a member in the project_membership table.
Output:
project_id - The id assigned to the project.
*/
function addProject($project_name,$time_created,$user_id,$description=null){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "INSERT INTO projects(project_id,project_name,
		time_created,user_id,description,deleted) 
		VALUES(null,'%s','%s',%d,'%s',0)";
		$replaced = sprintf($command,$project_name,$time_created,
		$user_id,$description);
		$db->queryExec($replaced);
		$command = "SELECT MAX(project_id) FROM projects";
		$result = $db->query($command);
		$cur = $result->current();
		$project_id = $cur['MAX(project_id)'];
		$command = "INSERT INTO project_membership(project_id,user_id,
		time_joined,deleted) 
		VALUES(%d,%d,'%s',0)";
		$replaced = sprintf($command,$project_id,$user_id,
		$time_created);
		$db->queryExec($replaced);
		return $project_id;
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project.
project_name - The name of this project.
project_name_edit - Set this to 1 to edit the name of the project, 
	0 otherwise.
description - A text description of the project.
description_edit - Set this to 1 to edit the description of the project,
	0 otherwise.
Process:
This function edits a project.
Output:
This function has no output.
*/
function editProject($project_id,$project_name,$project_name_edit,$description,
	$description_edit){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT COUNT(*) FROM projects
			WHERE project_id=%d AND deleted=0";
		$replaced = sprintf($command,$project_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']!=0){
			// This project exists
			$command = "UPDATE projects SET ";
			$added=false;
			if($project_name_edit!=null){
				$added=true;
				$command = $command."project_name='".
					$project_name."' ";
			}
			if($description_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."description='".
					$description."' ";
			}
			$command = $command."WHERE project_id=".$project_id.
				" AND deleted=0";
			$db->queryExec($command);
		} else {
			// This project does not exist,
			// don't do anything
		}
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project to be deleted.
Process:
This function deletes a project and its associated invitations, memberships
and tasks.
Output:
This function has no output.
*/
function deleteProject($project_id){
	// Delete all project invitations
	deleteAllProjectInvitationsFromProject($project_id);
	// Delete all project memberships
	deleteAllUsersFromProject($project_id);
	// Delete all project tasks
	deleteAllTasksFromProject($project_id);
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE projects SET deleted=1 WHERE project_id=%d";
		$replaced = sprintf($command,$project_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The id of the user.
project_id - The id of the project.
Process:
This function checks whether the user is a member of the project.
Output:
True if the user is in the project, false otherwise.
*/
function checkIsUserInProject($user_id,$project_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM project_membership WHERE
			user_id=%d AND project_id=%d AND deleted=0";
		$replaced = sprintf($command,$user_id,$project_id);
		$result = $db->query($replaced);
		if($result->valid()){
			return true;
		} else {
			return false;
		}
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project which the user is being invited to.
user_id - The id of the user who is being invited to the project.
inviter_id - The id of the person who invited the user to the project.
message - The text of the invitation.
time - The time the invitation was sent.
Process:
Adds an invitation to a user to the project_invitations table.
Output:
This function has no output.
*/
function addProjectInvitation($project_id,$user_id,$inviter_id,
$message,$time){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Check if this project invitation already exists
		$command = "SELECT COUNT(*) FROM project_invitations
			WHERE project_id=%d AND user_id=%d";
		$replaced = sprintf($command,$project_id,$user_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']==0){
			// This project invitation does not exist
			$command = "INSERT INTO project_invitations(project_id,
				user_id,inviter_id,message,time,deleted) 
				VALUES(%d,%d,%d,'%s','%s',0)";
			$replaced = sprintf($command,$project_id,$user_id,
				$inviter_id,$message,$time);
			$db->queryExec($replaced);
		} else {
			// This project invitation already exists,
			// check if it is deleted
			$command = "SELECT * FROM project_invitations
				WHERE project_id=%d AND user_id=%d";
			$replaced = sprintf($command,$project_id,$user_id);
			$result = $db->query($replaced);
			$cur = $result->current();
			if($cur['deleted']==1){
				// The project invitation is deleted
				$command = "UPDATE project_invitations
					SET inviter_id=%d,message='%s',
					time='%s',deleted=0
					WHERE project_id=%d AND user_id=%d";
				$replaced = sprintf($command,$inviter_id,
					$message,$time,$project_id,$user_id);
				$db->queryExec($replaced);
			} else {
				// The project invitation is not deleted,
				// don't do anything
			}
		}
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project for which this invitation will be deleted.
user_id - The id of the user for whom this project will be deleted.
Process:
This function deletes a project invitation for a particular user. Avoid using
this function if possible, instead use acceptProjectInvitation and
rejectProjectInvitation.
Output:
This function has no output.
*/
function deleteProjectInvitation($project_id,$user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Delete the project invitation
		$command = "UPDATE project_invitations SET deleted=1 
		WHERE project_id=%d AND user_id=%d";
		$replaced = sprintf($command,$project_id,$user_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project which the user is being invited to.
user_id - The id of the user who is being invited to the project.
time_accepted - The time the user accepted the invitation.
Process:
This function accepts an invitation to a project and adds the user to the
project_membership table. This function also deletes the project invitation.
Output:
This function has no output.
*/
function acceptProjectInvitation($project_id,$user_id,$time_accepted){
	deleteProjectInvitation($project_id,$user_id);
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Add user to project
		$command = "INSERT INTO project_membership(project_id,user_id,
		time_joined,deleted) 
		VALUES(%d,%d,'%s',0)";
		$replaced = sprintf($command,$project_id,$user_id,
			$time_accepted);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project which the user is being invited to.
user_id - The id of the user who is being invited to the project.
Process:
This function rejects an invitation to a project and deletes the project
invitation.
*/
function rejectProjectInvitation($project_id,$user_id){
	deleteProjectInvitation($project_id,$user_id);
}

/*
Input:
user_id - The user who is rejecting all project invitations.
Process:
This function rejects all project invitations for a user.
Output:
This function has no output.
*/
function deleteAllProjectInvitationsByUser($user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM project_invitations WHERE
			user_id=%d AND deleted=0";
		$replaced = sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteProjectInvitation($cur['project_id'],$user_id);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project which will have all of its invitations 
	deleted.
Process:
This function deletes all of the project invitations for a particular project.
Output:
This function has no output.
*/
function deleteAllProjectInvitationsFromProject($project_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM project_invitations WHERE
			project_id=%d AND deleted=0";
		$replaced = sprintf($command,$project_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteProjectInvitation($project_id,$cur['user_id']);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Input:
project_id - The id of the project.
user_id - The id of the user who is being removed from the project.
Process:
This function deletes a user from the project_membership table.
If there are no more users in the project, the project is deleted.
Output:
This function has no output.
*/
function deleteUserFromProject($project_id,$user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE project_membership SET deleted=1 
		WHERE project_id=%d AND user_id=%d";
		$replaced = sprintf($command,$project_id,$user_id);
		$db->queryExec($replaced);
		$command = "SELECT COUNT(*) FROM project_membership
		WHERE project_id=%d AND deleted=0";
		$replaced = sprintf($command,$project_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']==0){
			deleteProject($project_id);
		}
	} else {
		die($err);
	}
}

/*
Input:
user_id - The id of the user who is being removed from all projects.
Process:
This function deletes the user from all projects they are a member of.
Output:
This function has no output.
*/
function deleteUserFromAllProjects($user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM project_membership WHERE
			user_id=%d AND deleted=0";
		$replaced = sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteUserFromProject($cur['project_id'],$user_id);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project from which all users will be deleted.
Process:
This function deletes all users from a specific project.
Output:
This function has no output.
*/
function deleteAllUsersFromProject($project_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM project_membership WHERE
			project_id=%d AND deleted=0";
		$replaced = sprintf($command,$project_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteUserFromProject($project_id,$cur['user_id']);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Inputs:
meeting_name - The name of the meeting.
user_id - The id of the user who created the meeting.
duration_in_minutes - The suggested duration of the meeting in minutes.
location - The location of the meeting.
meeting_time - The final time decided on for the meeting.
meeting_description - A description of the meeting in text.
meeting_time_suggestion - A comma separated list of suggested meeting times.
Process:
This function adds a meeting to the meeting table and adds the meeting 
creator to the meeting_membership table.
Output:
meeting_id - The id of the meeting.
*/
function addMeeting($meeting_name,$user_id,$duration_in_minutes,$location,
$meeting_time=null,$meeting_description=null,$meeting_time_suggestion=null){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "INSERT INTO meetings(meeting_id,meeting_name,
		user_id,duration_in_minutes,location,meeting_time,
		meeting_description,meeting_time_suggestion,deleted) 
		VALUES(null,'%s',%d,%d,'%s','%s','%s','%s',0)";
		$replaced = sprintf($command,$meeting_name,$user_id,
			$duration_in_minutes,$location,$meeting_time,
			$meeting_description,$meeting_time_suggestion);
		$db->queryExec($replaced);
		$command = "SELECT MAX(meeting_id) FROM meetings";
		$result = $db->query($command);
		$cur = $result->current();
		$meeting_id = $cur['MAX(meeting_id)'];
		$command = "INSERT INTO meeting_membership(meeting_id,
		user_id,deleted) 
		VALUES(%d,%d,0)";
		$replaced = sprintf($command,$meeting_id,$user_id);
		$db->queryExec($replaced);
		return $meeting_id;
	} else {
		die($err);
	}
}

/*
Inputs:
meeting_id - The id of the meeting.
meeting_name - The name of this meeting.
meeting_name_edit - Set this to 1 to edit the name of the meeting, 
	0 otherwise.
duration_in_minutes - The suggested duration of the meeting in minutes.
duration_in_minutes_edit - Set this to 1 to edit the duration of the
	meeting, 0 otherwise.
location - The location of the meeting.
location_edit - Set this to 1 to edit the location of the meeting,
	0 otherwise.
meeting_time - The final time decided on for the meeting.
meeting_time_edit - Set this to 1 to edit the final meeting time,
	0 otherwise.
meeting_description - A description of the meeting in text.
meeting_description_edit - Set this to 1 to edit the meeting description,
	0 otherwise.
meeting_time_suggestion - A comma separated list of suggested meeting times.
meeting_time_suggestion_edit - Set this to 1 to edit the suggested meeting
	times, 0 otherwise.
Process:
This function edits a meeting.
Output:
This function has no output.
*/
function editMeeting($meeting_id,$meeting_name,$meeting_name_edit,
$duration_in_minutes,$duration_in_minutes_edit,$location,$location_edit,
$meeting_time,$meeting_time_edit,$meeting_description,$meeting_description_edit,
$meeting_time_suggestion,$meeting_time_suggestion_edit){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT COUNT(*) FROM meetings
			WHERE meeting_id=%d AND deleted=0";
		$replaced = sprintf($command,$meeting_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']!=0){
			// This meeting exists
			$command = "UPDATE meetings SET ";
			$added=false;
			if($meeting_name_edit!=null){
				$added=true;
				$command = $command."meeting_name='".
					$meeting_name."' ";
			}
			if($duration_in_minutes_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."duration_in_minutes='".
					$duration_in_minutes."' ";
			}
			if($location_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."location='".
					$location."' ";
			}
			if($meeting_time_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."meeting_time='".
					$meeting_time."' ";
			}
			if($meeting_description_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."meeting_description='".
					$meeting_description."' ";
			}
			if($meeting_time_suggestion_edit!=null){
				if($added==true){
					$command = $command.",";
				}
				$added=true;
				$command = $command."meeting_time_suggestion='".
					$meeting_time_suggestion."' ";
			}
			$command = $command."WHERE meeting_id=".$meeting_id.
				" AND deleted=0";
			$db->queryExec($command);
		} else {
			// This meeting does not exist,
			// don't do anything
		}
	} else {
		die($err);
	}
}

/*
Inputs:
meeting_id - The id of the meeting to be deleted.
Process:
This function deletes the meeting from the meeting table and deletes
all references to the meeting in the meeting_membership table.
Output:
This function has no output.
*/
function deleteMeeting($meeting_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE meetings SET deleted=1 WHERE meeting_id=%d";
		$replaced = sprintf($command,$meeting_id);
		$db->queryExec($replaced);
	} else {
		die($err);
	}
}

/*
Inputs:
meeting_id - The id of the meeting.
user_id - The id of the user who is being added to the meeting.
Process:
This function adds the user to the meeting_membership table.
Output:
This function has no output.
*/
function addUserToMeeting($meeting_id,$user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		// Check if this meeting membership already exists
		$command = "SELECT COUNT(*) FROM meeting_membership
			WHERE meeting_id=%d AND user_id=%d";
		$replaced = sprintf($command,$meeting_id,$user_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']==0){
			// This meeting membership does not exist
			$command = "INSERT INTO meeting_membership(meeting_id,
				user_id,deleted) VALUES(%d,%d,0)";
			$replaced = sprintf($command,$meeting_id,$user_id);
			$db->queryExec($replaced);
		} else {
			// This meeting membership already exists,
			// check whether it is deleted
			$command = "SELECT * FROM meeting_membership
				WHERE meeting_id=%d AND user_id=%d";
			$replaced = sprintf($command,$meeting_id,$user_id);
			$result = $db->query($replaced);
			$cur = $result->current();
			if($cur['deleted']==1){
				// This meeting membership is deleted
				$command = "UPDATE meeting_membership
					SET deleted=0
					WHERE meeting_id=%d AND user_id=%d";
				$replaced = sprintf($command,$meeting_id,
					$user_id);
				$db->queryExec($replaced);
			} else {
				// This meeting membership is not deleted
				// don't do anything
			}
		}
	} else {
		die($err);
	}
}

/*
Inputs:
meeting_id - The id of the meeting.
user_id - The id of the user who is being removed from the meeting.
Process:
This function removes a user from the meeting_membership table and deletes
the meeting if there are no more members in the meeting.
Output:
This function has no output.
*/
function deleteUserFromMeeting($meeting_id,$user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "UPDATE meeting_membership SET deleted=1 
		WHERE meeting_id=%d AND user_id=%d";
		$replaced = sprintf($command,$meeting_id,$user_id);
		$db->queryExec($replaced);
		$command = "SELECT COUNT(*) FROM meeting_membership
		WHERE meeting_id=%d AND deleted=0";
		$replaced = sprintf($command,$meeting_id);
		$result = $db->query($replaced);
		$cur = $result->current();
		if($cur['COUNT(*)']==0){
			deleteMeeting($meeting_id);
		}
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The user who is being deleted from all meetings.
Process:
This function removes a user from membership of all meetings.
Output:
This function has no output.
*/
function deleteUserFromAllMeetings($user_id){
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM meeting_membership WHERE
			user_id=%d AND deleted=0";
		$replaced = sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			deleteUserFromMeeting($cur['meeting_id'],$user_id);
			$result->next();
		}
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The user whose tasks are being displayed.
Process:
This function displays all of the user's tasks as an unordered list.
Includes tasks that the user created and tasks that the user is assigned.
Output:
This function has no return value.
This function prints an unordered list of the user's tasks.
*/
function displayTasks($user_id){
	if($db = new SQLiteDatabase('cs147organizer.db')){
		print "<ul>";
		$command = "SELECT * FROM tasks
		WHERE completed=0 AND deleted=0 AND (user_id=%d OR
		assigned_to=%d)";
		$replaced=sprintf($command,$user_id,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li>".$cur['task_name']."</li>\n";
			$result->next();
		}
		print "</ul>";
	} else {
		die($err);
	}
}

/*
Inputs:
task_id - The task for which comments are displayed.
Process:
This function displays all of the comments about a task as an unordered list.
Output:
This function has no return value.
This function prints an unordered list of the task's comments.
*/ 
function displayTaskComments($task_id){
	if($db = new SQLiteDatabase('cs147organizer.db')){
		print "<ul>";
		$command = "SELECT * FROM task_comments,users
		WHERE task_comments.deleted=0 AND users.deleted=0
		AND task_comments.user_id=users.user_id AND task_id=%d";
		$replaced=sprintf($command,$task_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li>".$cur['task_comments.comment']." - "
			.$cur['users.username'].
			"</li>\n";
			$result->next();
		}
		print "</ul>";
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The user whose projects are being displayed.
Process:
This funciton displays all of the user's projects as an unordered list.
Output:
This function has no return value.
This function prints an unordered list of the user's projects.
*/
function displayProjects($user_id){
	if($db = new SQLiteDatabase('cs147organizer.db')){
		print "<ul>";
		$command = "SELECT * FROM projects,project_membership
		WHERE projects.deleted=0 AND project_membership.deleted=0
		AND projects.project_id=project_membership.project_id 
		AND project_membership.user_id=%d";
		$replaced=sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li>".$cur['projects.project_name']."</li>\n";
			$result->next();
		}
		print "</ul>";
	} else {
		die($err);
	}
}

/*
Inputs:
project_id - The id of the project.
Process:
This function displays all of the project's tasks as an unordered list,
grouped by completed status.
Output:
This function has no return value.
This function prints an unordered list of the project's tasks.
*/
function displayProjectTasks($project_id){
	if($db = new SQLiteDatabase('cs147organizer.db')){
		print "<ul>";
		$command = "SELECT * FROM tasks
		WHERE deleted=0 AND project_id=%d";
		$replaced=sprintf($command,$project_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li>".$cur['task_name']."</li>\n";
			$result->next();
		}
		print "</ul>";
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The user whose project invitations are being displayed.
Process:
This function displays all project inviations for a user as an unordered list.
Output:
This function has no return value.
This function prints an unordered list of the user's project invitations.
*/
function displayProjectInvitations($user_id){
	if($db = new SQLiteDatabase('cs147organizer.db')){
		print "<ul>";
		$command = "SELECT * FROM projects,project_invitations
		WHERE projects.deleted=0 AND project_invitations.deleted=0 
		AND projects.project_id=project_invitations.project_id AND 
		project_invitations.user_id=%d";
		$replaced=sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li>".$cur['projects.project_name']."</li>\n";
			$result->next();
		}
		print "</ul>";
	} else {
		die($err);
	}
}

/*
Inputs:
user_id - The id of the project whose members are being displayed.
Process:
This funciton displays all of the project's members as an unordered list.
Output:
This function has no return value.
This function prints an unordered list of the project's members.
*/
function displayProjectMembers($project_id){
	if($db = new SQLiteDatabase('cs147organizer.db')){
		print "<ul>";
		$command = "SELECT * FROM users,project_membership
		WHERE users.deleted=0 AND project_membership.deleted=0
		AND users.user_id=project_membership.user_id 
		AND project_membership.project_id=%d";
		$replaced=sprintf($command,$project_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li>".$cur['users.username']."</li>\n";
			$result->next();
		}
		print "</ul>";
	} else {
		die($err);
	}
}

/*
Input:
user_id - The user whose meetings are being displayed.
Process:
This function displays the meetings a user is scheduled to attend
as an unordered list.
Output:
This function has no return value.
This function prints an unordered list of all of the user's scheduled meetings.
*/
function displayMeetings($user_id){
	if($db = new SQLiteDatabase('cs147organizer.db')){
		print "<ul>";
		$command = "SELECT * FROM meetings,meeting_membership
		WHERE meetings.deleted=0 AND meeting_membership.deleted=0
		AND meetings.meeting_id=meeting_membership.meeting_id
		AND meeting_membership.user_id=%d";
		$replaced=sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li>".$cur['meetings.meeting_name'];
			if(!empty($cur['meetings.location'])){
				print " at ".$cur['meetings.location'];
			}
			if($cur['meetings.duration_in_minutes']!=0){
				$format="%d";
				$replaced=sprintf($format,
					$cur['meetings.duration_in_minutes']);
				print " for ".$replaced." minutes";
			}
			if($cur['meetings.meeting_time']!=0){
				print " at ".$cur['meetings.meeting_time'];
			}
			print "</li>\n";
			$result->next();
		}
		print "</ul>";
	} else {
		die($err);
	}
}

/*
Input:
user_id
Process:
This function displays an unordered list of all of the user's contacts.
Output:
This function has no return value.
This function prints an unordered list of all of the user's contacts.
*/
function displayContacts($user_id){
	if($db = new SQLiteDatabase('cs147organizer.db')){
		print "<ul>";
		$command = "SELECT * FROM users,contact_list
		WHERE users.deleted=0 AND contact_list.deleted=0
		AND users.user_id=contact_list.contact_user_id
		AND contact_list.user_id=%d";
		$replaced=sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li>".$cur['users.username']." Tel: ".
			$cur['users.tel']." Mobile: ".$cur['users.mobile'].
			" Email: ".$cur['users.email']."</li>\n";
			$result->next();
		}
		print "</ul>";
	} else {
		die($err);
	}
}

/*
Inputs:
desired_duration - The desired duration of all tasks searched.
Process:
Produces a list of all tasks which fit in the desired duration of the task,
otherwise, produces a message stating that no tasks fit the duration.
Output:
This function outputs the list of tasks fitting the duration.
*/
function magicTodo($user, $desired_duration) {
	if($db = new SQLiteDatabase('cs147organizer.db')){
		$command = "SELECT * FROM tasks
		WHERE deleted=0 AND completed=0 AND disabled=0
		AND time_to_complete_in_minutes<=%d AND time_to_complete_in_minutes>0 AND (user_id=%d OR assigned_to=%d)";
		$replaced=sprintf($command,$desired_duration, $user, $user);
		$result = $db->query($replaced);
		if($result->valid()){
			while($result->valid()){
				$cur = $result->current();
				echo("<li class=\"magictask\">".stripslashes($cur['task_name']).
					"<span class=\"due" . 
					($cur['due_time_from'] < date('U') 
					? " overdue" : "")
                                        . "\">");
                		if(date('U',$cur['due_time_from']) < 2011996800)				{
                        		if(date('Ymd',$cur['due_time_from']) 
						== date('Ymd'))    {
                                		echo("Today");
                        		} else if(date('Ymd',
						$cur['due_time_from']) == 
						date('Ymd', (date('U') +
						86400))) {
                          			echo("Tomorrow");
                        		} else {
                                		echo($cur['due_time_from'] < (date('U') + 604800) && date('Ymd',$cur['due_time_from']) > date('Ymd', date('U'))  ? date('l', $cur['due_time_from']) : date('F j', $cur['due_time_from']));
                        		}
                		}
				echo("</span></li>\n");
				echo("<div class='spacer' />\n");
				$result->next();
			}
		} else {
			print "<li>No tasks fit within this duration</li>";
		}
	} else {
		die($err);
	}
}

?>
